package com.baidu.shoppingmall.comment.timedtask;

import com.baidu.shoppingmall.comment.domain.CommentInfoPO;
import com.baidu.shoppingmall.comment.service.CommentTimerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class CommentTimer {

    @Autowired
    private CommentTimerService commentTimerService;

    /**
     * 到达一定时间后执行一次相应的代码
     * @param timeout
     */
    public void checkValidityData(CommentInfoPO commentInfoPO,Long timeout){
        //定期检查刷新数据... 	 开启一个线程，检查有效期...(过期自动删除缓存)
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
        //定时长任务(到时间单次执行)
        scheduledThreadPool.schedule(new Runnable() {
            @Override
            public void run() {
                commentTimerService.timingComment(commentInfoPO);
                System.out.println("自动评论=========");
            }
        }, timeout, TimeUnit.MILLISECONDS);
    }
}
